package com.initialize.service.api.system.resttemplate.impl;

import com.initialize.commom.config.path.ServiceApiConfig;
import com.initialize.commom.config.resttemplate.RestTemplateConfig;
import com.initialize.commom.config.service.ServiceConfig;
import com.initialize.common.mybatis.plus.entity.User;
import com.initialize.service.api.system.entity.UserInfo;
import com.initialize.service.api.system.resttemplate.IUserRestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import static com.initialize.common.tool.util.CoreTool.createHttpHeaders;

/**
 * Created with IntelliJ IDEA.
 * User: jane
 * Date: 2019/12/31
 * Time: 4:58 下午
 * Description:
 */
@Slf4j
@Component
public class UserRestTemplateImpl implements IUserRestTemplate {

    final
    RestTemplate restTemplate;

    /**
     * feign
     */
    static String USER_API_PREFIX = RestTemplateConfig.HTTP + ServiceConfig.SERVICE_SYSTEM + ServiceApiConfig.USER_API_PREFIX;

    /**
     * controller
     */
    static String USER = RestTemplateConfig.HTTP + ServiceConfig.SERVICE_SYSTEM;

    public UserRestTemplateImpl(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Override
    public User login(String username) {
        HttpHeaders headers = createHttpHeaders();
        HttpEntity<String> stringHttpEntity = new HttpEntity<>(username, headers);


        return restTemplate.postForObject(
                USER_API_PREFIX + "/login",
                stringHttpEntity,
                User.class);
    }


    @Override
    public UserInfo findByUsername(String username) {
        HttpHeaders headers = createHttpHeaders();
        HttpEntity<String> stringHttpEntity = new HttpEntity<>(username, headers);

        return restTemplate.postForObject(
                USER + "/user/findByUsername",
                stringHttpEntity,
                UserInfo.class);
    }


}
